package com.tushikeji.facecar.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tushikeji.facecar.R;
import com.tushikeji.facecar.bean.UserBean;
import com.tushikeji.facecar.db.FaceDB;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;

public class FaceAdapter extends BaseAdapter {

    Context context;
    ArrayList<UserBean.User> faceData;

    public FaceAdapter(Context context, ArrayList<UserBean.User> faceData) {
        this.context = context;
        this.faceData = faceData;
    }

    @Override
    public int getCount() {
        return faceData.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public void RefrushData(ArrayList<UserBean.User> faceData) {
        this.faceData = faceData;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        holder = new ViewHolder();
        View convertViews = LayoutInflater.from(context).inflate(R.layout.face_item, null);
        holder.img = convertViews.findViewById(R.id.list01_item_img);
        holder.title = convertViews.findViewById(R.id.list01_item_txt);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(FaceDB.mFeatureStr+faceData.get(position).accountid);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Bitmap bitmap  = BitmapFactory.decodeStream(fis);
        holder.img.setImageBitmap(bitmap);
        holder.title.setText(faceData.get(position).name);
        holder.title.setPadding(15, 15, 15, 15);
        return convertViews;
    }


    static class ViewHolder {
        public ImageView img;
        public TextView title;
    }
}
